// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import axios from 'axios'
import store from './store'
import Vant from 'vant';
import { Toast } from 'vant';

import 'vant/lib/index.css';

Vue.use(Vant);


Vue.prototype.$store = store
Vue.prototype.$Toast = Toast

Vue.prototype.$axios = axios
axios.defaults.baseURL = '/api/v4'

axios.interceptors.request.use(req => {
  req.headers.authorization = localStorage.token
  return req
})

axios.interceptors.response.use(res => {
  res = res.data
  return res
})

axios.get('data/area').then(res => {
  if (res.errcode == 0) {
    store.areaData = res.data
  }
})

axios.get('user/info').then(res => {
  if (res.errcode == 0) {
    store.user = res.data
    for (let i = 0; i < store.user.addressList.length; i++) {
      store.user.addressList[i] = combineAddress(store.user.addressList[i])
    }
  }
})


Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})

function combineAddress(addr, type) {
  if (type) {
    addr.mobile = addr.tel
    addr.country = addr.county
    addr.detail = addr.addressDetail
    addr.code = addr.areaCode
  } else {
    addr.tel = addr.mobile
    addr.county = addr.country
    addr.addressDetail = addr.detail
    addr.areaCode = addr.code
  }
  addr.address = addr.province + addr.city + addr.country + addr.detail
  return addr
}

Vue.prototype.combineAddress = combineAddress